package Modes;

import java.util.ArrayList;
import java.util.List;

import Bacterias.Bacteria;
import GameElements.Coin;
import android.graphics.Canvas;
import android.util.Log;
import android.view.MotionEvent;

import com.game.ThauanFramework.GameObject;
import com.game.ThauanFramework.MinhasCoisas;
import com.game.ThauanFramework.TimeManager;
import com.thauanLopes.bacterium.GameSceneView;
import com.thauanLopes.bacterium.GameManages.GameManager;

public class NormalModeManager implements GameManager {

	private String TAG = "Smellie Manager";

	private float minInterval = 0.8f;
	private float maxInterval = 1.2f;
	
	private float nextInterval;

	private float intervalUpDificult = 10;
	private float difucultIncrease = 0.1f;
	
	private boolean upDificultOn = true;
	private int contadorDificuldade = 0;
	
	private int dificultCount = 9;

	private float cronometroNovaGosma;
	private float lifeCronometer = 0;

	private List<GameObject> cemetery;
	private List<GameObject> gameObjects;

	private float cronometroCoin = 0;
	private float coinChanceInterval = 3;

	public List<GameObject> getGameObjects() {
		return gameObjects;
	}

	private GameSceneView gameSceneView;

	public NormalModeManager(List<GameObject> cemetery,
			GameSceneView gameSceneView) {
		this.gameObjects = new ArrayList<GameObject>();

		this.cemetery = cemetery;

		this.gameSceneView = gameSceneView;

		this.nextInterval = NextInterval(minInterval, maxInterval);

		Log.i(TAG, "Normal Bacteria Manager - instanciado");
	}

	@Override
	public void Update(double deltaTime) {
		if (cronometroNovaGosma >= nextInterval) {
			Bacteria gosma = new Bacteria(gameObjects, cemetery,
					this.gameSceneView);
			cronometroNovaGosma = 0;

			this.nextInterval = NextInterval(this.minInterval, this.maxInterval);

		} else {
			cronometroNovaGosma += deltaTime;
		}

		this.lifeCronometer += deltaTime;
		if (lifeCronometer > this.intervalUpDificult && this.upDificultOn) {
			
			this.minInterval -= this.difucultIncrease;
			
			if (minInterval < 0) {
				minInterval = 0;
			}
			
			this.maxInterval -= this.difucultIncrease;
			
			Log.i(TAG,
					String.valueOf(this.minInterval) + ", "
							+ String.valueOf(this.maxInterval));
			contadorDificuldade++;
			if (contadorDificuldade >= this.dificultCount) {
				upDificultOn = false;
			}

			lifeCronometer = 0;
		}

		// ============================================================
		// Destroy elements no more used
		if (!cemetery.isEmpty()) {

			for (GameObject gb : cemetery) {
				gb.Destroy();
				gb = null;
			}
		}
		cemetery.clear();
		// ===========================================================

		if (!gameObjects.isEmpty()) {
			for (GameObject go : gameObjects) {
				go.Update(TimeManager.getInstance().getDeltaTime());
			}
		}

		// ----------------------- Coin
		if (this.cronometroCoin < this.coinChanceInterval) {
			this.cronometroCoin += deltaTime;
		}else {
			int coinLucky = MinhasCoisas.getRandom().nextInt(100);
			if (coinLucky >= 80) {
				Coin coin = new Coin(this.gameObjects, this.cemetery);
				cronometroCoin = 0;
			}
			
		}
		// -----------------------
	}

	// =======================================================================
	// DRAW
	@Override
	public void Draw(Canvas canvas) {
		if (!gameObjects.isEmpty()) {
			for (GameObject go : gameObjects) {
				go.Draw(canvas);

			}
		}
	}

	// ========================================================================
	// TOUCH
	@Override
	public boolean onTouch(MotionEvent event) {
		try {
			if (!gameObjects.isEmpty()) {
				for (GameObject gb : gameObjects) {
					gb.onTouch(event);
				}
			}
		} catch (Exception e) {
			Log.i(TAG, "erro ao toque");
		}
		
		return true;

	}

	private float NextInterval(float min, float max) {
		float interval = min + MinhasCoisas.getRandom().nextFloat()
				* (max - min);
		return interval;
	}

}
